<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #container {
      width: 750px;
      height: 291px;
      margin: 80px auto;
      position: relative;
    }

    #img-box img {
      display: none;
    }

    #img-box img.active {
      display: inline;
    }

    .btn {
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      width: 50px;
      height: 60px;
      background: rgba(0, 0, 0, 0.548);
      color: #fff;
      font-size: 50px;
      font-weight: bold;
      text-align: center;
      cursor: pointer;
      line-height: 60px;
    }

    #prev {
      left: 0;
    }

    #next {
      right: 0;
    }

    .tabs {
      position: absolute;
      right: 20px;
      bottom: 20px;
    }

    li {
      cursor: pointer;
      list-style-type: none;
      float: left;
      width: 30px;
      height: 30px;
      background: rgba(0, 0, 0, 0.493);
      color: #fff;
      margin-right: 10px;
      text-align: center;
      line-height: 30px;
    }

    .tabs li.active {
      background: rgba(255, 0, 0, 0.657);
    }
  </style>
</head>

<body>
  <div id="container">
    <div id="img-box">
      <img class="active" src="imgs/1.jpg" alt="">
      <img src="imgs/2.jpg" alt="">
      <img src="imgs/3.jpg" alt="">
      <img src="imgs/4.jpg" alt="">
      <img src="imgs/5.jpg" alt="">
    </div>
    <div class="btn-box">
      <div id="prev" class="btn">&lt;</div>
      <div id="next" class="btn">&gt;</div>
    </div>
    <ul class="tabs">
      <li class="active">1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
    </ul>
  </div>
  <script>
    // tab切换 == 选项卡
    var aLi = document.querySelectorAll('.tabs li'); // 数组
    var aImg = document.querySelectorAll('#img-box img')

    function change(oldIndex, newIndex) {
      aLi[oldIndex].className = '';
      aImg[oldIndex].className = '';

      aLi[newIndex].className = 'active';
      aImg[newIndex].className = 'active';
    }
    for (var i = 0; i < aLi.length; i++) {
      aLi[i].index = i;
      aLi[i].onclick = function () {
        // // 先把原有激活状态去掉
        // document.querySelector('.tabs .active').className = '';
        // document.querySelector('#img-box .active').className = '';
        // // 点谁给谁加active
        // this.className = 'active';
        // aImg[this.index].className = 'active';
        var oldIndex = document.querySelector('.tabs .active').index
        change(oldIndex, this.index)
      }
    }

    // 翻页
    var oNext = document.getElementById('next');
    oNext.onclick = function () {
      var current = document.querySelector('.tabs .active').index; // 0-4
      // aLi[current].className = '';
      // aImg[current].className = '';

      // aLi[current + 1].className = 'active';
      // aImg[current + 1].className = 'active';
      var newIndex
      if (current >= 4) {
        newIndex = 0
      } else {
        newIndex = current + 1
      }
      change(current, newIndex)
    }
    var oPrev = document.getElementById('prev');
    oPrev.onclick = function () {
      var current = document.querySelector('.tabs .active').index; // 0-4
      // aLi[current].className = '';
      // aImg[current].className = '';

      // aLi[current - 1].className = 'active';
      // aImg[current - 1].className = 'active';
      var newIndex
      if (current <= 0) {
        newIndex = 4
      } else {
        newIndex = current - 1
      }
      change(current, newIndex)
    }
    // 自动播放



    var timer = setInterval(function () {
      oNext.onclick()
    }, 1000)
    var oContainer = document.getElementById('container');
    oContainer.onmouseover = function () {
      clearInterval(timer)
    }
    oContainer.onmouseout = function () {
      timer = setInterval(function () {
        oNext.onclick()
      }, 1000)
    }
  </script>

</body>

</html>